package com.liukai.novel_type.presenter.impl;

import com.liukai.novel_base.base.BaseFragment;
import com.liukai.novel_base.base.BasePresenter;
import com.liukai.novel_commons.API;
import com.liukai.novel_commons.Result;
import com.liukai.novel_commons.utils.LogUtils;
import com.liukai.novel_commons.utils.RetrofitManager;
import com.liukai.novel_type.call_back.ITypePagerCallBack;
import com.liukai.novel_type.presenter.ITypePagerPresenter;

import javax.net.ssl.HttpsURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class TypePagerPresenterImpl extends BasePresenter<ITypePagerCallBack> implements ITypePagerPresenter {

    /**
     * 根据小说分类获取小说列表
     * @param typeId
     */
    @Override
    public void getNovelByType(Integer typeId) {
        Retrofit retrofit = RetrofitManager.getNovelInstance().getRetrofit();
        API api = retrofit.create(API.class);
        Call<Result> getNovelByTypeIdTask = api.getNovelByTypeId(typeId);
        getNovelByTypeIdTask.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                LogUtils.d(this.getClass(),"onResponse" + response.body());
                int code = response.code();
                // 网络请求成功
                if (code == HttpsURLConnection.HTTP_OK) {
                    mCallBack.setNovelByType(response.body());
                }
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });
    }

}
